package com.wooboo.server.ext.util;

import java.security.MessageDigest;

import org.apache.commons.codec.binary.Base64;


public final class MD5Util {
    /**
     * 获得加密后的16进制形式口令
     *
     * @param plainText
     * @return
     */
    public static String encrypt(String plainText) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(plainText.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        byte[] byteArray = messageDigest.digest();
        StringBuffer md5StrBuff = new StringBuffer();
        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                md5StrBuff.append("0").append(
                        Integer.toHexString(0xFF & byteArray[i]));
            else
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return md5StrBuff.toString().trim();
    }
    
    
    /**
	 * 简单异或加密算法，并进行base64加密
	 * 
	 * @param info 需要加密的串
	 * @param key  加密key
	 * @return
	 */
	public static String xorEncryAndBase64(String info, String key) {
		byte[] result = new byte[info.length()];

		for (int i = 0, j = 0; i < info.length(); ++i) {
			result[i] = (byte) (info.charAt(i) ^ key.charAt(j));
			j = (++j) % (key.length());
		}
		return Base64.encodeBase64String(result);
	}
    
	public static void main(String[] args) {
//		String info = "muid=0f074dc8e1f0547310e729032ac0730b&conv_time=1422263664&client_ip=10.11.12.13&sign=8a4d7f5323fd91b37430d639e6f7371b";
//		String key = "test_encrypt_key";
//		System.out.println(MD5Util.xorEncryAndBase64(info, key));
		
		String s = "C968AAB9-B323-463F-B261-FAD72EDAB73B";
		System.out.println(MD5Util.encrypt(s));
	}
	
}
